package indi.zhifa.recipe.bailan.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;


@Slf4j
public class LocalDateTimeUtil {
    static final DateTimeFormatter df;
    static {
        df = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral(' ')
                .append(DateTimeFormatter.ISO_LOCAL_TIME)
                .toFormatter();
    }
    /**
     * String时间转换成YYYY_MM_DD_HH_MM_SS类型的LocalDateTime
     *
     * @param source 字符串时间
     * @return LocalDateTime
     */
    public static LocalDateTime parse(String source) {

        LocalDateTime dateTime = null;
        try {
            //2020-01-01 00:00:00
            switch (source.length()) {
                case 7:
                    log.debug("传过来的是日期格式：{}", source);
                    source = source + "-01 00:00:00";
                    break;
                case 10:
                    log.debug("传过来的是日期格式：{}", source);
                    source = source + " 00:00:00";
                    break;
                case 13:
                    log.debug("传过来的是日期 小时格式：{}", source);
                    source = source + ":00:00";
                    break;
                case 16:
                    log.debug("传过来的是日期 小时:分钟格式：{}", source);
                    source = source + ":00";
                    break;
            }
            dateTime = LocalDateTime.parse(source, df);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return dateTime;
    }
    /**
     * LocalDateTime转换成YYYY-MM-DD类型的String
     *
     * @param source LocalDateTime时间
     * @return String
     */
    public static String formatYyyyMmDd(LocalDateTime source) {

        if (null == source) {
            return null;
        }

        String dateTime = null;
        try {
            DateTimeFormatter format = new DateTimeFormatterBuilder()
                    .parseCaseInsensitive()
                    .append(DateTimeFormatter.ISO_LOCAL_DATE).toFormatter();
            dateTime = source.format(format);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return dateTime;
    }

    /**
     * LocalDateTime转换成YYYY-MM-DD类型的String
     *
     * @param source    LocalDateTime时间
     * @param formatter 变换类型
     * @return String
     */
    public static String format(LocalDateTime source, String formatter) {
        if (null == source) {
            return null;
        }
        String dateTime = null;
        try {
            DateTimeFormatter format = DateTimeFormatter.ofPattern(formatter);
            dateTime = source.format(format);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return dateTime;
    }

    public static String toString(LocalDateTime pSource){
        return pSource.format(df);
    }


    public static Date localDateTimeToDate(final LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return null;
        }
        final ZoneId zoneId = ZoneId.systemDefault();
        final ZonedDateTime zdt = localDateTime.atZone(zoneId);
        final Date date = Date.from(zdt.toInstant());
        return date;
    }
    public static LocalDateTime dateToLocalDateTime(final Date date) {
        if (null == date) {
            return null;
        }
        final Instant instant = date.toInstant();
        final ZoneId zoneId = ZoneId.systemDefault();
        final LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }
}
